package per.srp.demo.carLink;

import per.srp.demo.carLink.entity.Car;
import per.srp.demo.carLink.entity.CarAndSocket;
import per.srp.demo.carLink.entity.CarCommand;
import per.srp.demo.carLink.log.CarLogger;
import per.srp.demo.carLink.log.LogType;

import javax.management.remote.rmi._RMIConnection_Stub;
import java.io.IOException;
import java.net.Socket;
import java.util.LinkedList;

public class CarHandler {

    private CarAndSocket carAndSocket;
    public CarHandler(CarAndSocket carAndSocket){
        this.carAndSocket = carAndSocket;
    }

    public LinkedList<CarCommand> submitCarPackage() throws IOException {
        Socket carSocket = carAndSocket.getSocket();
        Car car = carAndSocket.getCar();

        String ip = carSocket.getInetAddress().toString();
        String logInfo = car.getNo()+"("+car.getLon()+","+car.getLat()+")"+car.getV();

        //记录日志
        CarLogger.log(LogType.LINK_SUCCESSFULLY,carSocket,logInfo);

        //插入连接小车数据
        CarDataManager.insert(carAndSocket);

        //获取当前小车对应执行指令
        return CarCommandHandler.popCarCommand(car);
    }
}
